package com.omega_r.omegarecyclerview.pagination_example;

import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.omegarecyclerview.ResourceTable;
import ohos.agp.components.Component;
import ohos.app.Context;

import java.util.List;

public class ImageProvider<H extends Object> extends BaseProvider {
    private Component progress;
    private Component error;
    private boolean type=false;

    public ImageProvider(Context context, List providerList, int layoutId) {
        super(context, providerList, layoutId);

    }


    @Override
    public void setFooterComponent(Component footer) {
        super.setFooterComponent(footer);
        if (footer!=null) {
            progress = footer.findComponentById(ResourceTable.Id_progress);
            error = footer.findComponentById(ResourceTable.Id_error);
            if (providerList.size()==0){
                progress.setVisibility(Component.VISIBLE);
                error.setVisibility(Component.HIDE);
                mCallback.onRetryClicked();
            }
            footer.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    System.out.println("==========="+type);
                    mCallback.onRetryClicked();
                    if (type){
                        progress.setVisibility(Component.VISIBLE);
                        error.setVisibility(Component.HIDE);
                    }else {
                        progress.setVisibility(Component.HIDE);
                        error.setVisibility(Component.VISIBLE);
                    }
                }
            });
        }
    }

    @Override
    public void addAllProviderList(List list) {
        if (providerList.size()==0) {
            progress.setVisibility(Component.HIDE);
            error.setVisibility(Component.VISIBLE);
        }
        super.addAllProviderList(list);
    }

    public void setError() {
        type=true;
    }

    public void setProgress() {
        type=false;
    }

    private Callback mCallback;

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    interface Callback {
        void onRetryClicked();
    }

}
